﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

using Peer;

namespace MicroMq.Core
{
    /// <summary>
    /// 生产者
    /// </summary>
    public class MicroProducer
    {
        private readonly PeerClient peerClient;

        internal MicroProducer(PeerClient peerClient)
        {
            this.peerClient = peerClient;
        }
        /// <summary>
        /// 同步发送
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="data"></param>
        public void Send(string topic, object data)
        {
            SendAsync(topic, data).GetAwaiter().GetResult();
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public Task SendAsync(string topic, object data)
        {
            return peerClient.SendAsync(MqBuilder.Publish(topic, MTP.Serialize.MTPSerialize.Serialize(data)));
        }
        /// <summary>
        /// 延时发送
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="delay">延迟时间  秒</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public Task SendDelay(string topic, int delay, object data)
        {
            return peerClient.SendAsync(MqBuilder.Publish(topic, delay, MTP.Serialize.MTPSerialize.Serialize(data)));
        }
    }
}
